<?php
/**
 * Created by PhpStorm.
 * User: zd
 * Date: 2018/12/1
 * Time: 11:32
 */

namespace app\api\service;


use app\api\model\CardAudio;
use app\api\model\CardBase;
use app\api\model\CardExtend;
use app\api\model\CardImg;
use app\api\model\CardProduct;
use app\api\model\CardTag;
use app\api\model\CardVideo;
use app\api\model\WorkUser;
use think\Db;
use think\Exception;

class Enterprise
{
    /**
     * 更具id获取用户信息
     * @param $uid
     * @return array|false|\PDOStatement|string|\think\Model
     */
        public function getUserInfo($uid){
            $user = WorkUser::getUserByWuID($uid);
            $info = $user->visible(['id','name','ai_power','boss_power','head_img','style','position','userimg','thirdapp']);
            $info->thirdapp->visible(['name','head_img']);
            return $info;
        }
    /**
     * 更新用户头像
     * @param $img
     * @param $wu_id
     * @return $this
     */
        public function updateImg($img,$wu_id){
            $user = WorkUser::updateImg($img,$wu_id);
            return $user;
        }

    /**
     * 新华扬CRM用户基本信息
     * @param $uid
     * @return array|false|\PDOStatement|string|\think\Model
     */
       public function getBaseInfo($uid){
            $user = WorkUser::getBaseInfo($uid);
            return $user;
       }

    /**
     * 获取新华扬CRM名片基本信息
     * @param $w_id
     * @return null|static
     */
       public function getCardBase($w_id){
            $baseInfo = new CardBase();
            $info =  $baseInfo->getCardBase($w_id);
            return $info;
       }

    /**
     * 更新新华扬CRM名片基本信息
     * @param $info
     * @return false|int
     */
       public function updateCardBase($info,$wu_id){
           $card = new CardBase();
           $info = $card->updateCard($info,$wu_id);
           return $info;
       }
    /**
     * 更新用户电话信息
     * @param $info
     * @param $wu_id
     * @return $this
     */
       public function updatePhone($info,$wu_id){
           $card = new CardBase();
           $data = $card->updatePhone($info,$wu_id);
           return $data;
       }
    /**
     * 获取新华扬CRM用户已有推荐商品
     * @param $wu_id
     * @return false|\PDOStatement|string|\think\Collection
     */
       public function getGoods($wu_id){
           $card = new CardProduct();
           $info =  $card->getGoods($wu_id);
           return $info;
       }

    /**
     * 新华扬CRM添加推荐商品
     * @param $info
     * @param $wu_id
     * @return false|int
     */
       public function addGoods($info,$wu_id){
           $card = new CardProduct();
           $data='';
           if ($info != null){
               foreach ($info as $key=>$value){
                   foreach ($value as $i=>$j){
                   $data[]['p_id'] = $j['goods_id'];
                   }
               }
           }
           $result =  $card->addGoods($data,$wu_id);
           return $result;
       }

    /**
     * 获取新华扬CRM用户拓展信息
     * @param $wu_id
     * @return array
     */
       public function getExtendInfo($wu_id){
           $extendInfo = CardExtend::getInfo($wu_id);
           $tagInfo = CardTag::getTag($wu_id);
           $imgInfo = CardImg::getImgByUid($wu_id);
           $info = [$extendInfo,$tagInfo,$imgInfo];
           return $info;
       }
    /**
     * 新华扬CRM添加拓展信息
     * @param $info
     * @param $wu_id
     * @return array|false|int
     * @throws Exception
     */
       public function addExtendInfo($info,$wu_id){
           $data='';
           $data['abstract'] = $info['synopsis'][0];
           if (!empty($info['audio'])){
               $data['audio_id'] = $info['audio'][0]['audio_id'];
               if (!empty($info['audio'][0]['oldAudio_id'])){//删除原有音频
                   $fileInfo = CardAudio::getImg($info['audio'][0]['oldAudio_id']);
                   $file = new FileUpload();
                   $result =  $file->deleteQiniuFile($fileInfo->key);
                   if (empty($result)){
                       CardAudio::deleteImg($info['audio'][0]['oldAudio_id']);
                   }
               }
           }
           if (!isset($info['synopsis'][1])){//新增
               Db::startTrans();
               try {
                   $extend = CardExtend::addInfo($data,$wu_id);
                   $extend = CardTag::addTag($info['label'],$wu_id);
                   Db::commit();
               }catch (Exception $exception){
                   Db::rollback();
                   throw  $exception;
               }
           }else{//更新
               $data['id'] = $info['synopsis'][1];
               Db::startTrans();
               try {
                   $extend = CardExtend::updateInfo($data,$wu_id);
                   $extend = CardTag::updateTag($info['label'],$wu_id);
                   Db::commit();
               }catch (Exception $exception){
                   Db::rollback();
                   throw  $exception;
               }
           }
           return $extend;
       }
    /**
     * 获取视频信息
     * @param $w_id
     * @return array|false|\PDOStatement|string|\think\Model
     */
       public function getVideoInfo($w_id){
           $video = CardVideo::getVideoInfo($w_id);
            return $video;
       }

    /**
     * 更新分享语
     * @param $info
     * @param $wu_id
     * @return false|int
     */
       public function updateShareWord($info,$wu_id){
           $data['id'] = $info['id'];
           $data['share_word'] = $info['share_word'];
           if ($info['id'] == 0){
               $word = CardExtend::addInfo($data,$wu_id);
           }else{
               $word = CardExtend::updateInfo($data,$wu_id);
           }
           return $word;
       }
    /**
     * 分享语设置
     * @param $wu_id
     * @return $this
     */
       public function getShareWord($wu_id){
           $word = CardExtend::getShareWord($wu_id);
           return $word;
       }
    /**
     * 获取欢迎语
     * @param $wu_id
     * @return array|false|\PDOStatement|string|\think\Model
     */
       public function getWelcomeWord($wu_id){
           $word = CardExtend::getWelcomeWord($wu_id);
           return $word;
       }
    /**
     * 更新欢迎语
     * @param $info
     * @param $wu_id
     * @return false|int
     */
    public function updateWelcomeWord($info,$wu_id){
        $data['id'] = $info['id'];
        $data['welcome_word'] = $info['welcome_word'];
        if ($info['id'] == 0){
            $word = CardExtend::addInfo($data,$wu_id);
        }else{
            $word = CardExtend::updateInfo($data,$wu_id);
        }
        return $word;
    }
}